package leetcode.easy.q06;

import java.util.ArrayList;
import java.util.List;

class Solution {
    public String convert(String s, int numRows) {
        if (numRows==1){
            return s;
        }
        List<StringBuilder> list=new ArrayList<>();
        for (int i=0;i<numRows;i++){
            list.add(new StringBuilder());
        }
        boolean down=true;
        for (int i = 0,y=-1; i <s.length() ;) {
            char c= s.charAt(i);
            while (down&&i <s.length() ){
                c= s.charAt(i);
                y++;
                list.get(y).append(c);
                i++;
                //i指向下一个，y还在上一个
                if (y==numRows-1) {
                    down=false;
                }
            }
            while ( !down&&i <s.length()){
                c= s.charAt(i);
                y--;
                list.get(y).append(c);
                i++;
                if (y==0){
                    down=true;
                }
            }
        }
        StringBuilder sb= new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
//        System.out.println(String.format("%8s","c"));
//        System.out.println(String.format("%7s","c"));
//        System.out.println(String.format("%6s","c"));
//        System.out.println(String.format("%12c",'c'));
        System.out.println(new Solution().convert("PAYPALISHIRING",
                4));
    }
}